# 有语的第一个项目实践，外星人大战项目
# 记录得分
# 22/10/6

import pygame.font


class Scoreboard:
    """显示得分"""

    def __init__(self, ai_game, stats):
        """初始化相关属性"""
        self.high_score_rect = None
        self.high_score_image = None
        self.score_str = None
        self.score_rect = None
        self.score_image = None
        self.screen = ai_game.screen
        self.screen_rect = ai_game.screen.get_rect()
        self.settings = ai_game.settings
        self.score = stats.score
        self.high_score = stats.high_score
        # 显示得分信息使用的字体设置
        self.text_color = (100, 100, 100)
        self.font = pygame.font.SysFont(None, 48)
        # 准备初始得分图像
        self.prep_score(stats.score)
        self.prep_high_score()


    def prep_score(self, score):
        """把得分转换为一幅渲染的图像"""
        self.score = score
        self.score_str = str(score)
        self.score_image = self.font.render(self.score_str, True,
                                            self.text_color,
                                            self.settings.bg_color)
        # 在屏幕右上角显示得分
        self.score_rect = self.score_image.get_rect()
        self.score_rect.right = self.screen_rect.right - 20
        self.score_rect.top = 20

    def show_score(self):
        """在屏幕上显示得分"""
        self.screen.fill(self.settings.bg_color, self.score_rect)
        self.screen.blit(self.score_image, self.score_rect)
        self.screen.blit(self.high_score_image, self.high_score_rect)

    def prep_high_score(self):
        high_score = round(self.high_score, -1)
        high_score_str = "{:,}".format(high_score)
        self.high_score_image = self.font.render(
            high_score_str, True,
            self.text_color, self.settings.bg_color
        )

        # 把最高分放在屏幕中央
        self.high_score_rect = self.high_score_image.get_rect()
        self.high_score_rect.centerx = self.screen_rect.centerx
        self.high_score_rect.top = self.score_rect.top

    def check_high_score(self):
        """检查是否诞生心的最高分"""
        # self.high_score = high_score

        if self.score >= self.high_score:
            self.high_score = self.score
            self.prep_high_score()
